package turistic.GUI.graphic;

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.StringTokenizer;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;

public class GoogleMapsActivity extends MapActivity {
	/** Called when the activity is first created. */
	MapView myMapView = null;
	MapController myMC = null;
	GeoPoint geoPoint = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.maps);

		Intent sender = getIntent();
		String x = sender.getExtras().getString("Coordinates");
		StringTokenizer st = new StringTokenizer(x, " ");
		double xx = Double.parseDouble(st.nextToken());
		double yy = Double.parseDouble(st.nextToken());

		myMapView = (MapView) findViewById(R.id.mapview);
		LinearLayout zoomLayout = (LinearLayout) findViewById(R.id.zoom);
		View zoomView = myMapView.getZoomControls();

		zoomLayout.addView(zoomView, new LinearLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		myMapView.displayZoomControls(true);

		geoPoint = null;
		myMapView.setSatellite(false);
		double fromLat = xx;// 46.771144;
		double fromLong = yy;// 23.602409;
		double toLat = 47.186213;
		double toLong = 23.055496;

		String sourceLat = Double.toString(fromLat);
		String sourceLong = Double.toString(fromLong);
		String destinationLat = Double.toString(toLat);
		String destinationLong = Double.toString(toLong);

		String pairs[] = getDirectionData(sourceLat, sourceLong,
				destinationLat, destinationLong);
		String[] lngLat = pairs[0].split(",");

		GeoPoint startGP = new GeoPoint(
				(int) (Double.parseDouble(lngLat[1]) * 1E6),
				(int) (Double.parseDouble(lngLat[0]) * 1E6));

		myMC = myMapView.getController();
		geoPoint = startGP;
		myMC.setCenter(geoPoint);
		myMC.setZoom(10);
		myMapView.getOverlays().add(new DirectionPathOverlay(startGP, startGP));

		// NAVIGATE THE PATH

		GeoPoint gp1;
		GeoPoint gp2 = startGP;

		for (int i = 1; i < pairs.length; i++) {
			lngLat = pairs[i].split(",");
			gp1 = gp2;
			// watch out! For GeoPoint, first:latitude, second:longitude

			gp2 = new GeoPoint((int) (Double.parseDouble(lngLat[1]) * 1E6),
					(int) (Double.parseDouble(lngLat[0]) * 1E6));
			myMapView.getOverlays().add(new DirectionPathOverlay(gp1, gp2));
			Log.d("xxx", "pair:" + pairs[i]);
		}

		// END POINT
		myMapView.getOverlays().add(new DirectionPathOverlay(gp2, gp2));

		myMapView.getController().animateTo(startGP);
		myMapView.setBuiltInZoomControls(true);
		myMapView.displayZoomControls(true);

	}

	/*
	 * List<Overlay> mapOverlays = mapView.getOverlays(); Drawable drawable =
	 * this.getResources().getDrawable(R.drawable.ic_launcher); ItemizedOverlay
	 * itemizedoverlay = new ItemizedOverlay(drawable, this); GeoPoint point =
	 * new GeoPoint(19240000,-99120000); OverlayItem overlayitem = new
	 * OverlayItem(point, "Hola, Mundo!", "I'm in Mexico City!");
	 * itemizedoverlay.addOverlay(overlayitem);
	 * mapOverlays.add(itemizedoverlay); GeoPoint pointt = new
	 * GeoPoint(18240000,-98120000); OverlayItem overlayitemm = new
	 * OverlayItem(pointt, "Hola, Mundo!", "I'm in Mexico City!");
	 * itemizedoverlay.addOverlay(overlayitemm);
	 * mapOverlays.add(itemizedoverlay);
	 */

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	private String[] getDirectionData(String sourceLat, String sourceLong,
			String destinationLat, String destinationLong) {

		String urlString = "http://maps.google.com/maps?f=d&hl=en&" + "saddr="
				+ sourceLat + "," + sourceLong + "&daddr=" + destinationLat
				+ "," + destinationLong + "&ie=UTF8&0&om=0&output=kml";
		Log.d("URL", urlString);
		Document doc = null;
		HttpURLConnection urlConnection = null;
		URL url = null;
		String pathConent = "";

		try {

			url = new URL(urlString.toString());
			urlConnection = (HttpURLConnection) url.openConnection();
			urlConnection.setRequestMethod("GET");
			urlConnection.setDoOutput(true);
			urlConnection.setDoInput(true);
			urlConnection.connect();
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			doc = db.parse(urlConnection.getInputStream());

		} catch (Exception e) {
		}

		NodeList nl = doc.getElementsByTagName("LineString");
		for (int s = 0; s < nl.getLength(); s++) {
			Node rootNode = nl.item(s);
			NodeList configItems = rootNode.getChildNodes();
			for (int x = 0; x < configItems.getLength(); x++) {
				Node lineStringNode = configItems.item(x);
				NodeList path = lineStringNode.getChildNodes();
				pathConent = path.item(0).getNodeValue();
			}
		}
		String[] tempContent = pathConent.split(" ");
		return tempContent;
	}

}
